package com.tadpole.music.adapter.me;

import android.app.Activity;
import android.provider.VoicemailContract;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.material.tabs.TabLayout;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.TabAdapter;
import com.tadpole.music.bean.me.MoNiErrorBean;
import com.tadpole.music.view.fragment.me.MoNiV2Fragment;
import com.tadpole.music.view.fragment.me.ZhenTiV2Fragment;
import com.tadpole.music.view.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By 孙岩
 * on 2021/1/29
 */
public class AnswerStatisticsV3Adapter extends BaseQuickAdapter<MoNiErrorBean.DataBean, BaseViewHolder> {
    //tabLayout
    private TabLayout tabLayout;
    //viewPager
    private NoScrollViewPager viewPager;
    private TabAdapter tabAdapter;
    //tab集合
    private List<String> tabList ;
    //viewpager绑定的fragment的集合
    private List<Fragment> fragmentList  ;
    private FragmentManager fragmentManager;

    private TextView tvTitle;
    //学生id
    private int student_id;
    //学生头像
    private String student_avatar;
    //学生昵称
    private String student_nickname;
    private Activity activity;

    public AnswerStatisticsV3Adapter(Activity activity,int layoutResId, @Nullable List<MoNiErrorBean.DataBean> data, FragmentManager fragmentManager, int student_id, String student_avatar, String student_nickname) {
        super(R.layout.item_big, data);
        this.activity = activity;
        this.fragmentManager = fragmentManager;
        this.student_id = student_id;
        this.student_avatar = student_avatar;
        this.student_nickname = student_nickname;
    }

    @Override
    protected void convert(BaseViewHolder helper, MoNiErrorBean.DataBean item) {
        tabLayout = helper.itemView.findViewById(R.id.tabLayout);
        viewPager = helper.itemView.findViewById(R.id.viewPager);
        tvTitle = helper.itemView.findViewById(R.id.tvTitle);

        tvTitle.setText(item.getName());

        initTab(helper.getLayoutPosition(),item);
    }

    private void initTab(int position,MoNiErrorBean.DataBean item){
        tabList =  new ArrayList<>();
        fragmentList = new ArrayList<>();
        tabList.add("模拟训练");
        tabList.add("实战真题");
        fragmentList.add(MoNiV2Fragment.newInstance(item, student_id + "", student_avatar, student_nickname));
        fragmentList.add(ZhenTiV2Fragment.newInstance(item.getRank_id() + "", student_id + ""));

        tabAdapter = new TabAdapter(fragmentList, tabList, fragmentManager, activity);
        viewPager.setCurrentItem(0);
        viewPager.setId(position + 1);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(tabAdapter);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(tabAdapter.getTabView(i));
            }

            if (i == 0) {
                //设置默认进来第一个是选中状态
                TabLayout.Tab a = tabLayout.getTabAt(0);
                View view1 = a.getCustomView();
                if (null != view1 && view1 instanceof TextView) {
                    ((TextView) view1).setTextSize(16);
                    ((TextView) view1).setTextColor(activity.getResources().getColor(R.color.color_33));
                }
            } else {
                //设置默认进来第二个是不选中状态
                TabLayout.Tab b = tabLayout.getTabAt(1);
                View view2 = b.getCustomView();
                if (null != view2 && view2 instanceof TextView) {
                    ((TextView) view2).setTextSize(13);
                    ((TextView) view2).setTextColor(activity.getResources().getColor(R.color.color_65));
                }
            }
        }


        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //切换导航菜单时候的监听
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (viewPager != null) {
                    viewPager.setCurrentItem(tab.getPosition(), true);
                    View view = tab.getCustomView();
                    if (null != view && view instanceof TextView) {
                        ((TextView) view).setTextSize(16);
                        ((TextView) view).setTextColor(activity.getResources().getColor(R.color.color_33));
                        ((TextView) view).setTextAppearance(activity, R.style.TabLayoutTextStyle);
                    }
                }


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (viewPager != null) {
                    View view = tab.getCustomView();
                    if (null != view && view instanceof TextView) {
                        ((TextView) view).setTextSize(13);
                        ((TextView) view).setTextColor(activity.getResources().getColor(R.color.color_65));
                    }
                }
            }


            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
}
